properties {
  $base_dir = resolve-path .
  
  $source_dir = "$base_dir\Source"
  $solution_file = "$source_dir\PSSymbolStore.sln"
  
  $xunit = "$source_dir\External Dependencies\xunit\xunit.console.exe"
  
  if ($build_configuration -ieq "release")
  {
    $build_dir = "$base_dir\build\release"
    $solution_params = "/p:Configuration=Release;OutDir=$build_dir\;OutputPath=$build_dir\"
  }
  else
  {
    $build_dir = "$base_dir\build\debug"
    $solution_params = "/p:Configuration=Debug;OutDir=$build_dir\;OutputPath=$build_dir\"
  }
  
  $diagnostics_dir = "$source_dir\PSSymbolStore.Diagnostics"
  $winsdk_dir = "C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin"
  $sdmc = "$winsdk_dir\TSPDesigner\sdmc.exe"
  $sdcatalog = "$winsdk_dir\TSPDesigner\sdcatalog.exe"
  $makecat = "$winsdk_dir\makecat.exe"
  $signtool = "$winsdk_dir\signtool.exe"
}

task default -depends Test, Troubleshooting

task Test -depends Compile { 
  & $xunit "$build_dir\PSSymbolStore.Tests.dll"
  stopBuildIfError
  & $xunit "$build_dir\PSSymbolStore.Manager.Tests.dll"
  stopBuildIfError
}

task Compile -depends Clean { 
  msbuild "$solution_file" "$solution_params"
  stopBuildIfError
}

task Clean { 
  remove-item -force -recurse $build_dir -ErrorAction SilentlyContinue | out-null
  msbuild "$solution_file" /target:Clean $solution_params
  stopBuildIfError
}

task Troubleshooting -depends CleanTroubleshooting {

  mkdir "$diagnostics_dir\output\"
  $original_dir = (pwd)
  cd $diagnostics_dir

  & "$makecat" -v PSSymbolStore.cdf
  stopBuildIfError
  
  & "$signtool" sign -a -n TestCertforWindowsTroubleshooting -s TestWindowsTroubleshooting DiagPackage.cat
  stopBuildIfError
  
  move DiagPackage.cat output\
  copy PSSymbolStore.diagpkg output\
  copy *.ps1 output\

  cd $original_dir
  
  mkdir $build_dir\diagnostics\
  copy $diagnostics_dir\output\*.* $build_dir\diagnostics\

}

task CleanTroubleshooting {
  if (Test-Path "$diagnostics_dir\output\") { remove-item -force -recurse "$diagnostics_dir\output\" }
  if (Test-Path "$build_dir\diagnostics\") { remove-item -force -recurse "$build_dir\diagnostics\" }
}


###### FUNCTIONS ########

function stopBuildIfError
{
  if ($lastexitcode -ne 0) {
    throw "Build BREAK"
  }
}